//#include "tk_dlm.hpp"
#include "..\src\yolo_v2_class.hpp"
#include <cuda_runtime.h>


tk_dlm::tk_dlm()
	:m_pdt(0)
{

}

tk_dlm::~tk_dlm()
{
	Release();
}

bool tk_dlm::Load(const char *config_file, const char *weight_file, int gpu)
{ 
	Release();
	m_pdt = new Detector(std::string(config_file), std::string(weight_file), gpu);
	return true;
}

void tk_dlm::Release()
{
	if (m_pdt)
	{
		delete m_pdt;
		m_pdt = 0;
	}
}

int tk_dlm::Detect(image_rgbf_t img, bbox_t * p_result_boxs, float thresh, bool use_mean)
{
	if (!m_pdt || !p_result_boxs)
	{
		return 0;
	}
	std::vector<bbox_t> ret = m_pdt->detect(img, thresh, use_mean);
	int ret_size = int(__min(C_SHARP_MAX_OBJECTS, ret.size()));
	if (ret_size)
	{
		memcpy(p_result_boxs, &ret[0], sizeof(bbox_t)*ret_size);
	}
	return ret_size;
}

// int tk_dlm::Resize(image_rgb16_t img, image_t img_resize)
// {
// 	st_cuda_resize_dev_space img_resize_space;
// 	img_resize.w = 416;
// 	img_resize.h = 416;
// 	CuImgResizeMalloc(img_resize_space, __max(img.w, img_resize.w)*__max(img.h, img_resize.h));
// 	CuImgResizeRgb16(img.data, img.w, img.h, img_resize_space, 416, 416);
// 	
// 	//img_resize.data = img_resize_space.img_rgbf_resized;
// 	cudaMemcpy(img_resize.data, img_resize_space.img_rgbf_resized, 416 * 416 * sizeof(float) * 3, cudaMemcpyDeviceToHost);
// 	return 0;
// }

int tk_dlm::Detect(image_rgb8_t img, bbox_t * p_result_boxs, float thresh, bool use_mean)
{
	if (!m_pdt || !p_result_boxs)
	{
		return 0;
	}
	std::vector<bbox_t> ret = m_pdt->detect(img, thresh, use_mean);
	int ret_size = int(__min(C_SHARP_MAX_OBJECTS, ret.size()));
	if (ret_size)
	{
		memcpy(p_result_boxs, &ret[0], sizeof(bbox_t)*ret_size);
	}
	return ret_size;
}

int tk_dlm::Detect(image_rgb16_t img, bbox_t * p_result_boxs, float thresh, bool use_mean)
{
	if (!m_pdt || !p_result_boxs)
	{
		return 0;
	}
	std::vector<bbox_t> ret = m_pdt->detect(img, thresh, use_mean);
	int ret_size = int(__min(C_SHARP_MAX_OBJECTS, ret.size()));
	if (ret_size)
	{
		memcpy(p_result_boxs, &ret[0], sizeof(bbox_t)*ret_size);
	}
	return ret_size;
}

int tk_dlm::Detect(const char * img_file, bbox_t * p_result_boxs, float thresh, bool use_mean)
{
	if (!m_pdt || !p_result_boxs)
	{
		return 0;
	}
	std::vector<bbox_t> ret = m_pdt->detect(std::string(img_file), thresh, use_mean);
	int ret_size = int(__min(C_SHARP_MAX_OBJECTS, ret.size()));
	if (ret_size)
	{
		memcpy(p_result_boxs, &ret[0], sizeof(bbox_t)*ret_size);
	}
	return ret_size;
}
